﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace homework3
{


    internal class Program
    {

        static void Main(string[] args)
        {
            Random rd = new Random();
            int[] arr = new int[100];
            for (int i = 0; i < 100; i++)
            {
                arr[i] = rd.Next(0, 1000);
            }
            var longwords = from asa in arr orderby asa descending select asa;
            var sum_ = longwords.Sum();
            var avg_ = longwords.Average();
            Console.WriteLine(sum_);
            Console.WriteLine(avg_);
            foreach (var longword in longwords)
            {
                Console.WriteLine(longword);
            }
            //  Console.WriteLine(longwords);
            OrderService a = new OrderService();
             bool judge_ = true;
            while (judge_)
            {
                Console.WriteLine("输入1增加订单，输入2删除订单，输入3查询订单，输入4显示所有订单，输入5为订单排序,输入6修改，输入7退出");
                string choose1 = Console.ReadLine();
                switch (choose1)
                {
                    case "1": a.add(); break;
                    case "2": a.removeOrder(); break;
                    case "3": a.search(); break;
                    case "4": a.show(); break;
                    case "5": a.OrderIt(); break;
                    case "6": a.modify(); break;
                    case "7": judge_ = false; break;
                    default: Console.WriteLine("输入错误"); break;
                }
            }

            Console.ReadLine();
        }
    }

    class OrderService
    {
        public List<Order> orders = new List<Order>();
        public OrderService() { }
        public void show()
        {
            foreach (Order b3 in orders)
            {
                Console.WriteLine(b3.ToString());
                b3.showOrderItem();
                Console.WriteLine("----------------------------");

            }
        }

        public void add()
        {
            try
            {
                Console.WriteLine("请输入订单编号");
                int id = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("请输入客户");
                string customer = Console.ReadLine();
                Console.WriteLine("请输入订单项");
                Order a = new Order(id, customer);
                foreach (Order m in this.orders)
                {
                    if (m.Equals(a))
                    {
                        Console.WriteLine("重复");
                        return;
                    }
                }
                while (true)
                {
                    Console.WriteLine("请输入物品名称：");
                    string name = Console.ReadLine();
                    Console.WriteLine("请输入购买数量：");
                    int number = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("请输入单价：");
                    double price = Convert.ToDouble(Console.ReadLine());
                    a.addOrderItem(name, number, price);
                    Console.WriteLine("是否继续添加订单项：");
                    string x = Console.ReadLine();
                    if (x == "否") break;
                    else if (x == "是") continue;
                    else if (x != "否" && x != "是")
                    {
                        Exception e = new Exception();
                        throw e;
                    }
                }
                orders.Add(a);
                a.getAllPrice();
                Console.WriteLine("建立成功");
                Console.WriteLine("-------------------------");
            }
            catch { Console.WriteLine("输入有误"); }
        }
        public void search()
        {
            try
            {
                Console.WriteLine("请输入查询的的依据：1、订单号.2、商品名称。3、客户。4、金额范围");
                int i = Convert.ToInt32(Console.ReadLine());
                switch (i)
                {
                    case 1:
                        Console.WriteLine("输入订单号");
                        int id = Convert.ToInt32(Console.ReadLine());
                        var q1 = (from o in orders
                                  where o.Id == id
                                  select o).ToList();
                        Order b2 = q1.First();
                        Console.WriteLine("{0} {1} {2}", b2.Id, b2.Customer, b2.Money);
                        b2.showOrderItem();
                        break;
                    case 2:
                        Console.WriteLine("输入商品名称");
                        string name = Console.ReadLine();
                        var q2 = (from o in orders
                                  where (from item in o.OrderItems
                                         where item.NAME == name
                                         select item).Count() > 0
                                  orderby o.Money
                                  select o).ToList();
                        foreach (Order b3 in q2)
                        {
                            Console.WriteLine("订单号 客户 总金额");
                            Console.WriteLine("----------------------------");
                            Console.WriteLine("{0} {1} {2}", b3.Id, b3.Customer, b3.Money);
                            b3.showOrderItem();
                        }
                        break;
                    case 3:
                        Console.WriteLine("输入客户名称");
                        string name3 = Console.ReadLine();
                        var q3 = (from o in orders
                                  where o.Customer == name3
                                  orderby o.Money
                                  select o).ToList();
                        foreach (Order b3 in q3)
                        {
                            Console.WriteLine("订单号 客户 总金额");
                            Console.WriteLine("----------------------------");
                            Console.WriteLine("{0} {1} {2}", b3.Id, b3.Customer, b3.Money);
                            b3.showOrderItem();
                        }
                        break;
                    case 4:
                        int minNum, maxNum;
                        Console.WriteLine("输入要查询的最小金额：");
                        minNum = Convert.ToInt32(Console.ReadLine());
                        Console.WriteLine("输入要查询的最大金额：");
                        maxNum = Convert.ToInt32(Console.ReadLine());


                        var query1 = from s1 in orders
                                     where maxNum > s1.Money
                                     orderby s1.Money
                                     select s1;
                        var query3 = from s3 in query1
                                     where s3.Money > minNum
                                     orderby s3.Money
                                     select s3;

                        List<Order> a1 = query3.ToList();

                        foreach (Order b1 in a1)
                        {
                            Console.WriteLine("订单号 客户 总金额");
                            Console.WriteLine("----------------------------");
                            Console.WriteLine("{0} {1} {2}", b1.Id, b1.Customer, b1.Money);
                            b1.showOrderItem();
                        }
                        break;
                    default: Console.WriteLine("输入错误"); break;

                }
            }
            catch
            {

            }
        }
        public void removeOrder()           //删除订单
        {
            try
            {
                Console.WriteLine("输入订单号删除订单或相应明细：");
                int id = Convert.ToInt32(Console.ReadLine());
                int index = 0;
                foreach (Order a in this.orders)
                {
                    if (a.Id == id) index = this.orders.IndexOf(a);
                }
                Console.WriteLine("输入1删除订单，输入2继续删除订单明细");
                int choose = Convert.ToInt32(Console.ReadLine());
                switch (choose)
                {
                    case 1: this.orders.RemoveAt(index); Console.WriteLine("删除成功"); Console.WriteLine("-----------------"); break;
                    case 2: this.orders[index].showOrderItem(); this.orders[index].RemoveOrderItem(); break;
                    default: Console.WriteLine("输入错误"); break;
                }
            }
            catch
            {
                Console.WriteLine("输入错误");
            }

        }
        public void OrderIt()
        {
            Console.WriteLine("请输入排序依据:1、订单号，2、总金额");
            int id = Convert.ToInt32(Console.ReadLine());
            switch(id)
            {
                case 1:
                    orders.Sort((Order x,Order y)=>x.CompareTo(y));
                    break;
                case 2:
                    orders.Sort((Order x, Order y) => x.Money.CompareTo( y.Money));
                    break;
                default:break;
            }
        }

        public void modify()
        {
           Console.WriteLine("请自行删除再添加,谢谢");
        }
    }
    class Order : IComparable
    {
        public int Id { get; set; }
        public string Customer { get; set; }
        public double Money { get; set; }
        public List<OrderDetails> OrderItems = new List<OrderDetails>();

        public new string ToString()
        {
            return "订单号:" + Id + "；客户：" + Customer + ",金额:" + Money;

        }
        public int CompareTo(object obj)
        {
            Order a = obj as Order;
            return this.Id.CompareTo(a.Id);
        }

        public void getAllPrice()
        {
            foreach (OrderDetails o in this.OrderItems)
            {
                Money += o.getPrice();
            }
        }
        public void addOrderItem(string name, int number, double price)
        {
            OrderDetails o = new OrderDetails(name, number, price);
            this.OrderItems.Add(o);
        }
        public override bool Equals(object obj)
        {
            Order a = obj as Order;
            if (a.Id == this.Id) return true;
            else return false;
        }

        public void showOrderItem()
        {
            foreach (OrderDetails a in this.OrderItems)
            {
                Console.WriteLine(a.ToString());
            }
        }

        public void RemoveOrderItem()
        {
            try
            {
                Console.WriteLine("请输入订单明细序号删除相应订单明细：");
                int a = Convert.ToInt32(Console.ReadLine());
                this.OrderItems.RemoveAt(a);
                Console.WriteLine("删除成功");
                Console.WriteLine("-------------------------");
            }
            catch
            {
                Console.WriteLine("输入序号错误");
            }
        }

        public Order(int id, string customer)
        {
            this.Id = id;
            this.Customer = customer;
            this.Money = 0;
        }

    }

    class OrderDetails
    {
        private string name;
        public string NAME
        {
            get { return name; }
            set { name = value; }
        }
        private int number;
        public int NUMBER
        {
            get { return number; }
            set { number = value; }
        }
        private double price;
        public double PRICE
        {
            get { return price; }
            set { price = value; }
        }
        public new string ToString()
        {
            return "商品:" + NAME + "数量" + NUMBER + "单价" + price;
        }
        public OrderDetails(string name, int number, double price)
        {
            this.name = name;
            this.number = number;
            this.price = price;
        }
        public OrderDetails()
        {
            this.name = string.Empty;
            this.number = 0;
            this.price = 0;
        }
        public double getPrice()
        {
            return this.price * this.number;
        }
    }


}